package com.ruoyi.slip.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.ruoyi.slip.domain.SalarySlipVO;
import com.ruoyi.slip.domain.tax;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.slip.mapper.SalarySlipMapper;
import com.ruoyi.slip.domain.SalarySlip;
import com.ruoyi.slip.service.ISalarySlipService;

/**
 * 员工工资条明细Service业务层处理
 * 
 * @author cxs
 * @date 2025-05-30
 */
@Service
public class SalarySlipServiceImpl implements ISalarySlipService 
{
    @Autowired
    private SalarySlipMapper salarySlipMapper;

    /**
     * 查询员工工资条明细
     * 
     * @param id 员工工资条明细主键
     * @return 员工工资条明细
     */
    @Override
    public SalarySlip selectSalarySlipById(Long id)
    {
        return salarySlipMapper.selectSalarySlipById(id);
    }

    /**
     * 查询员工工资条明细列表
     * 
     * @param salarySlipvo 员工工资条明细
     * @return 员工工资条明细
     */
    @Override
    public List<SalarySlipVO> selectSalarySlipList(SalarySlipVO salarySlipvo)
    {
        return salarySlipMapper.selectList(salarySlipvo);
    }

    /**
     * 新增员工工资条明细
     * 
     * @param salarySlip 员工工资条明细
     * @return 结果
     */
    @Override
    public int insertSalarySlip(SalarySlip salarySlip)
    {
        return salarySlipMapper.insertSalarySlip(salarySlip);
    }

    /**
     * 修改员工工资条明细
     * 
     * @param salarySlip 员工工资条明细
     * @return 结果
     */
    @Override
    public int updateSalarySlip(SalarySlip salarySlip)
    {
        return salarySlipMapper.updateSalarySlip(salarySlip);
    }

    /**
     * 批量删除员工工资条明细
     * 
     * @param ids 需要删除的员工工资条明细主键
     * @return 结果
     */
    @Override
    public int deleteSalarySlipByIds(Long[] ids)
    {
        return salarySlipMapper.deleteSalarySlipByIds(ids);
    }

    /**
     * 删除员工工资条明细信息
     * 
     * @param id 员工工资条明细主键
     * @return 结果
     */
    @Override
    public int deleteSalarySlipById(Long id)
    {
        return salarySlipMapper.deleteSalarySlipById(id);
    }

    @Override
    public int calculateSalary(List<Long> ids) {

        int influence = 0;

        for (Long id : ids) {

            //查询基本薪资
            Double totalSalary = salarySlipMapper.selectBaseSalaryById(id);

            //查询这个月请了几天假--一天扣50
            // 获取当前日期
            LocalDate today = LocalDate.now();
            // 获取月初（当月的第一天）
            LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
            LocalDateTime startOfMonth = firstDayOfMonth.atStartOfDay();
            // 获取月末（当月的最后一天）
            LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
            LocalDateTime endOfMonth = lastDayOfMonth.atTime(23, 59, 59, 999999999);
            //  查询这个月请了几天假
            Double vocation = salarySlipMapper.selectVocationById(id, startOfMonth, endOfMonth);
            //计算请假扣除工资
            Double deductVocation = vocation * 50;

            //查询这个月加了几个小时的班 -- 加班一个小时50
            Double plusHours = salarySlipMapper.selectPlusHoursById(id,startOfMonth,endOfMonth);
            //计算加班工资
            Double plusHoursSalary = plusHours * 50;

            //计算个税
            List<tax> taxes = salarySlipMapper.selectPersonalTaxById(id);
            Double personal = 0.0;
            Double gjj = 0.0;
            for (tax tax : taxes){
                 personal += tax.getPersonalBase() * tax.getPersonalRatio() / 100;
                  gjj += tax.getGjjBase() * tax.getGjjRatio() / 100;
            }

            //计算总工资
            Double realSalary = totalSalary + plusHoursSalary - deductVocation - personal - gjj;

            Long mainId = salarySlipMapper.selectMainId(id);
            SalarySlip salarySlip = new SalarySlip();
            salarySlip.setId(mainId);
            salarySlip.setOvertimeHours(BigDecimal.valueOf(plusHours));
            salarySlip.setLeaveHours( BigDecimal.valueOf(vocation));
            salarySlip.setBasicSalary(BigDecimal.valueOf(totalSalary));
            salarySlip.setOvertimeAllowance(BigDecimal.valueOf(plusHoursSalary));
            salarySlip.setLeaveDeduction(BigDecimal.valueOf(deductVocation));
            salarySlip.setIncomeTax(BigDecimal.valueOf(personal));
            salarySlip.setSocialInsurance(BigDecimal.valueOf(gjj));
            salarySlip.setTotalSalary(BigDecimal.valueOf(realSalary));

            influence += salarySlipMapper.updateSalarySlip(salarySlip);

        }
        return  influence;
    }
}
